package com.lancoo.answer.view.fragment.dictation;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.lancoo.answer.R;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.Child;
import com.lancoo.answer.model.bean.Type;
import com.lancoo.answer.util.SoftInputUtils;
import com.lancoo.answer.view.fragment.composition.answer.CompositionPictureAnswerFragment;
import com.lancoo.answer.view.fragment.composition.answer.CompositionVideoAnswerFragment;
import com.lancoo.answer.view.fragment.dictation.answer.PassageDictationPencilAnswerFragment;
import com.lancoo.answer.widget.combinationView.CompositionQueseView;
import com.lancoo.answer.widget.combinationView.QueseBodyView;

import java.util.List;

/**
 * Created by pengmi on 2021/12/10 15:02
 * Description:短文听写作答fragment
 */
public class PassageDictationAnswerFragment extends Fragment {


    private Child child;
    private View convertView;
    private static final String TAG = "CompositionAnswerFragme";
    private int currentAnswerType=-1;
    private int kindIndex;
    private int queseIndex;
    private int smallQueseIndex;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        convertView=inflater.inflate(R.layout.ev_fragment_passage_dictation_answer_layout,container,false);
        return convertView;
    }


    public static Fragment loadQueseFragment(int kindIndex, int queseIndex, int smallQueseIndex){

        PassageDictationAnswerFragment queseFragment=new PassageDictationAnswerFragment();
        Bundle mBundle=new Bundle();
        mBundle.putInt("kindIndex",kindIndex);
        mBundle.putInt("queseIndex",queseIndex);
        mBundle.putInt("smallQueseIndex",smallQueseIndex);
        queseFragment.setArguments(mBundle);
        return queseFragment;
    }



    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        adjustFontSize();
    }

    private void initView(View view){
        Log.e(TAG,"initView"+(child==null));
        if(getArguments()==null){
            return;
        }
        kindIndex = getArguments().getInt("kindIndex");
        queseIndex = getArguments().getInt("queseIndex");
        smallQueseIndex = getArguments().getInt("smallQueseIndex");

        List<Type> kindBeanListk = ConstantBean.Companion.getConstantExamBean().getTypeList();
        child = kindBeanListk.get(kindIndex).getQuesList().get(queseIndex).getChildList().get(smallQueseIndex);
        CompositionQueseView queseView=view.findViewById(R.id.composition_quese_view);
        queseView.findViewById(R.id.tv_quese_body).setVisibility(View.INVISIBLE);
//        queseView.setCloseAudioAnswer(true);
        queseView.setCloseVideoAnswer(true);
        queseView.setCloseTxtAnswer(true);
        queseView.setOnAnswerTypeChangeCallback(new CompositionQueseView.OnAnswerTypeChangeCallback() {
            @Override
            public void onAnswerTypeSelected(int position) {
                super.onAnswerTypeSelected(position);
                loadAnswerMethod(position);
            }
        });
        queseView.intData(child);
        QueseBodyView queseBodyView = view.findViewById(R.id.quese_body_view);
        queseBodyView.initData(child);

    }

    private void loadAnswerMethod(int answerType){
        Log.e(TAG,"initView");
        FragmentManager fragmentManager = getChildFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment fragment= getChildFragmentManager().findFragmentByTag(answerType+"");//getFragmentManager()
        Fragment lastFragment= getChildFragmentManager().findFragmentByTag(currentAnswerType+"");//getFragmentManager()
        if(lastFragment!=null){
            fragmentTransaction.hide(lastFragment);
        }
        if(getActivity()!=null)
            SoftInputUtils.hideSoftInput(getActivity());
        if(fragment==null){
            switch (answerType){
//                case 1:
//                    fragment= CompositionAudioAnswerFragment.loadFragment(kindIndex,queseIndex,smallQueseIndex);
//                    break;
                case 2:
                    fragment= CompositionPictureAnswerFragment.loadFragment(kindIndex,queseIndex,smallQueseIndex,0);
                    break;
                case 3:
                    fragment= CompositionVideoAnswerFragment.loadFragment(kindIndex,queseIndex,smallQueseIndex);
                    break;
                default:
                    fragment= PassageDictationPencilAnswerFragment.loadFragment(kindIndex,queseIndex,smallQueseIndex);
            }
            fragmentTransaction.add(R.id.framelayout,fragment,answerType+"");
        }else{
            fragmentTransaction.show(fragment);
        }
        fragmentTransaction.commitAllowingStateLoss();
        currentAnswerType=answerType;
    }

    public void enableAnswer(){
        Log.e("1122","短文听写作答");
        if(getContext()==null){
            return;
        }
        FragmentManager childFragmentManager = getChildFragmentManager();
        List<Fragment> fragments = childFragmentManager.getFragments();
        for (Fragment fragment:fragments){
            if(fragment instanceof PassageDictationPencilAnswerFragment){
                ((PassageDictationPencilAnswerFragment)fragment).enableAnswer();
            }
        }
    }

    public void adjustFontSize(){
        if(convertView==null){
            return;
        }
        QueseBodyView queseBodyView=convertView.findViewById(R.id.quese_body_view);
        queseBodyView.adjustFontSize();
        CompositionQueseView compositionQueseView=convertView.findViewById(R.id.composition_quese_view);
        compositionQueseView.adjustFontSize();
    }

    public void markQuestion(){
        if(convertView==null){
            return;
        }
        QueseBodyView queseBodyView=convertView.findViewById(R.id.quese_body_view);
        queseBodyView.markQuestion();
    }
}
